package com.zhiche.lisa.bms.controller.utils;

import com.google.common.collect.Lists;
import com.zhiche.lisa.bms.dao.model.invoice.InvoiceApply;
import com.zhiche.lisa.bms.service.arfund.ArFundService;
import com.zhiche.lisa.bms.service.bill.BillIoDetailService;
import com.zhiche.lisa.bms.service.bill.BillService;
import com.zhiche.lisa.bms.service.fee.FeeAssessmentService;
import com.zhiche.lisa.bms.service.fee.FeePayableService;
import com.zhiche.lisa.bms.service.fee.FeeRewardService;
import com.zhiche.lisa.bms.service.invoice.InvoiceApplyService;
import com.zhiche.lisa.bms.service.pay.PayApplyService;
import com.zhiche.lisa.bms.service.prepay.PrepayBillService;
import com.zhiche.lisa.bms.service.price.PriceIncomingService;
import com.zhiche.lisa.bms.service.price.PricePurchaseService;
import com.zhiche.lisa.bms.service.psup.*;
import com.zhiche.lisa.bms.service.quote.QuoteDtlService;
import com.zhiche.lisa.bms.service.sta.StaPriceService;
import com.zhiche.lisa.bms.service.utils.LspUtil;
import com.zhiche.lisa.bms.service.utils.common.Cache;
import com.zhiche.lisa.bms.service.utils.common.ExcelUtil;
import com.zhiche.lisa.bms.service.utils.common.MD5Util;
import com.zhiche.lisa.bms.service.utils.common.identityCodeUtil.CodeUtil;
import com.zhiche.lisa.core.enums.CommonEnum;
import com.zhiche.lisa.core.supports.RestfulResponse;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Map;

/**
 * @author: linbao
 * @date: 2019-04-26
 */
public class BaseExcelController {

    /**
     * excel资源缓冲区
     */
    protected final static Cache<HSSFWorkbook> RESOURCES = new Cache<HSSFWorkbook>();

    /**
     * 10-承运商
     */
    public final static Integer LSP_INFO_NAME = 10;
    /**
     * 20-车队
     */
    public final static Integer LISA_FLEET_NAME = 20;
    /**
     * 30-司机
     */
    public final static Integer LISA_DRIVER_NAME = 30;

    /**
     * 统一配置字段映射结果
     */
    @Autowired
    protected PriceIncomingService priceIncomingService;

    @Autowired
    protected PricePurchaseService pricePurchaseService;

    @Autowired
    protected BillService billService;

    @Autowired
    protected BillIoDetailService billIoDetailService;

    @Autowired
    protected ArFundService arFundService;

    @Autowired
    protected FeeAssessmentService feeAssessmentService;

    @Autowired
    protected LspUtil lspUtil;

    @Autowired
    protected PayApplyService payApplyService;

    @Autowired
    protected FeePayableService feePayableService;

    @Autowired
    protected PrepayBillService prepayBillService;

    @Autowired
    protected FeeRewardService feeRewardService;

    @Autowired
    protected StaPriceService staPriceService;

    @Autowired
    protected QuoteDtlService quoteDtlService;

    @Autowired
    protected PsupBaseDataService psupBaseDataService;

    @Autowired
    protected PsupTraditionPriceService psupTraditionPriceService;

    @Autowired
    protected PsupFixedPriceService psupFixedPriceService;

    @Autowired
    protected PsupVehicleContrastService psupVehicleContrastService;

    @Autowired
    protected PsupOtherExpensisesService psupOtherExpensisesService;

    @Autowired
    protected InvoiceApplyService invoiceApplyService;

    @Autowired
    protected CodeUtil codeUtil;

    protected boolean verifyStringData(String data, RestfulResponse<Object> result) {
        boolean resultBoolean = false;
        if (StringUtils.isEmpty(data)) {
            result.setCode(CommonEnum.ERROR.getCode());
            result.setMessage("识别不到数据, 请检查文件或数据格式!");
            resultBoolean = true;
        }
        return resultBoolean;
    }

    /**
     * 校验数据是否识别到
     *
     * @param dataEntity
     * @param result
     * @return
     */
    protected boolean verifyDataEntity(List dataEntity, RestfulResponse<Object> result) {
        boolean resultBoolean = false;
        if (CollectionUtils.isEmpty(dataEntity)) {
            result.setCode(CommonEnum.ERROR.getCode());
            result.setMessage("识别不到数据, 请检查文件或数据格式!");
            resultBoolean = true;
        }
        return resultBoolean;
    }

    /**
     * 校验数据是否为空的异常信息
     *
     * @param resultStr
     * @param result
     * @return
     */
    protected boolean verifyErrorList(List<String> resultStr, RestfulResponse<Object> result) {
        boolean resultBoolean = false;
        if (!CollectionUtils.isEmpty(resultStr)) {
            result.setCode(CommonEnum.ERRORLIST.getCode());
            result.setMessage(CommonEnum.ERRORLIST.getText());
            result.setData(resultStr);
            resultBoolean = true;
        }
        return resultBoolean;
    }

    /**
     * 通用excel导出
     *
     * @param excelName
     * @param titles
     * @param dataList
     * @param contractMap
     * @param otherKey    导出的文件名
     * @return
     */
    protected RestfulResponse commonExportExcelResult(String excelName, String[] titles, List dataList,
                                                      Map<String, Object> contractMap, String otherKey) {
        if (CollectionUtils.isEmpty(dataList)) {
            dataList = Lists.newArrayList();
        }
        HSSFWorkbook excel = ExcelUtil.export(excelName, titles, dataList, contractMap);
        String key = MD5Util.backMD5(excel.toString());
        if (!StringUtils.isEmpty(otherKey)) {
            key += otherKey;
        }
        RESOURCES.put(key, excel);
        return new RestfulResponse<String>(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getText(), key);
    }
}
